package com.love_weinuo.ui.activity;

import android.annotation.SuppressLint;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.love_weinuo.R;
import com.love_weinuo.databinding.ActivityWebviewBinding;
import com.love_weinuo.ui.BaseActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2019/3/18.
 */

public class WebViewActivity extends BaseActivity {

    ActivityWebviewBinding binding;
    List<String> list = new ArrayList<>();
    String webUrl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_webview);
        binding.setActiviy(this);
        initView();
    }

    @SuppressLint("JavascriptInterface")
    private void initView() {
        add(this);
        setBack();
        setTitleText("滴滴打药");
        webUrl = getIntent().getStringExtra("module_web_url");
        binding.webView.getSettings().setDomStorageEnabled(true);
        binding.webView.getSettings().setAppCacheMaxSize(1024 * 1024 * 8);
        String appCachePath = getApplicationContext().getCacheDir()
                .getAbsolutePath();
        binding.webView.getSettings().setAppCachePath(appCachePath);
        binding.webView.getSettings().setAllowFileAccess(true);
        binding.webView.getSettings().setAppCacheEnabled(true);
        binding.webView.getSettings().setJavaScriptEnabled(true);
        binding.webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        binding.webView.addJavascriptInterface(WebViewActivity.this, "android");
        binding.webView.setWebViewClient(new MyWebViewClient());
        binding.webView.loadUrl(webUrl);
    }

    public class MyWebViewClient extends WebViewClient {
        @Override
        public void onPageFinished(WebView view, String url) {
            binding.webView.loadUrl("javascript:setDataList('" + list.toString() + "')");
        }
    }
}
